﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace BUS
{
    public class CCorlorRichTextBox
    {
        private struct KeyDaNhan
        {
            public string KeyName;
            public int Start;
            public int Length;
 
        }

        public static void Highlight(object sender)
        {
            RichTextBox richTextBox = (RichTextBox)sender;
            Highlight(richTextBox);
        }
        public static void Highlight(RichTextBox richTextBox)
        {
            ArrayList _DaNhan = new ArrayList();
            KeyDaNhan _Key = new KeyDaNhan();
            int _start = richTextBox.SelectionStart;
            int _length = richTextBox.SelectionLength;

            richTextBox.SelectionStart = 0;
            for (int i = 0; i < richTextBox.Text.Length; i++)
            {
                if (richTextBox.Text[i] == '<')
                {
                    richTextBox.SelectionLength = i - richTextBox.SelectionStart;
                    richTextBox.SelectionColor = Color.Black;
                    Boolean _KeyUp = false;
                    if (richTextBox.Text[i + 1] == '/')
                    {
                        i++;
                        _KeyUp = true;
                    }
                    richTextBox.SelectionStart = i + 1;
                    for (i++; i < richTextBox.Text.Length; i++)
                    {
                        if (richTextBox.Text[i] == '>')
                        {
                            richTextBox.SelectionLength = i - richTextBox.SelectionStart;
                            _Key.KeyName = richTextBox.SelectedText;
                            if (_KeyUp)
                            {
                                _Key.Start = richTextBox.SelectionStart -= 2;
                                _Key.Length = richTextBox.SelectionLength += 3;
                                if (CTransferKeysCode.SwithCode(_Key.KeyName) != -1)
                                {
                                    bool _flag = true;
                                    for (int j = 0; j < _DaNhan.Count; j++)
                                    {
                                        KeyDaNhan _temp = (KeyDaNhan)_DaNhan[j];
                                        if (_Key.KeyName == _temp.KeyName)
                                        {
                                            richTextBox.SelectionColor = Color.SkyBlue;
                                            _DaNhan.RemoveAt(j);
                                            _flag = false;
                                            break;
                                        }
                                    }
                                    if (_flag)
                                        richTextBox.SelectionColor = Color.Red;
                                }
                                else
                                {
                                    richTextBox.SelectionColor = Color.Red;
                                }
                            }
                            else
                            {
                                _Key.Start = richTextBox.SelectionStart--;
                                _Key.Length = richTextBox.SelectionLength += 2;
                                if (CTransferKeysCode.SwithCode(_Key.KeyName) != -1)
                                {
                                    bool _flag = true;
                                    for (int j = 0; j < _DaNhan.Count; j++)
                                    {
                                        KeyDaNhan _temp = (KeyDaNhan)_DaNhan[j];
                                        if (_Key.KeyName == _temp.KeyName)
                                        {
                                            richTextBox.SelectionColor = Color.Red;
                                            _flag = false;
                                            break;
                                        }
                                    }
                                    if (_flag)
                                    {
                                        richTextBox.SelectionColor = Color.SkyBlue;
                                        _DaNhan.Add(_Key);
                                    }
                                }
                                else
                                {
                                    richTextBox.SelectionColor = Color.Red;
                                }
                            }
                            richTextBox.SelectionStart += richTextBox.SelectionLength;
                            //richTextBox.SelectionLength = 0;
                            break;
                        }
                    }
                }
                else if (richTextBox.Text[i] == '(')
                {
                    richTextBox.SelectionLength = i - richTextBox.SelectionStart;
                    richTextBox.SelectionColor = Color.Black;
                    richTextBox.SelectionStart = i + 1;
                    for (i++; i < richTextBox.Text.Length; i++)
                    {
                        if (richTextBox.Text[i] == ')')
                        {
                            richTextBox.SelectionLength = i - richTextBox.SelectionStart;
                            if (CTransferKeysCode.SwithCode(richTextBox.SelectedText) != -1)
                            {
                                bool _flag = true;
                                for (int j = 0; j < _DaNhan.Count; j++)
                                {
                                    KeyDaNhan _temp = (KeyDaNhan)_DaNhan[j];
                                    if (richTextBox.SelectedText == _temp.KeyName)
                                    {
                                        richTextBox.SelectionStart--;
                                        richTextBox.SelectionLength += 2;
                                        richTextBox.SelectionColor = Color.Red;
                                        _flag = false;
                                        break;
                                    }
                                }
                                if (_flag)
                                {
                                    richTextBox.SelectionStart--;
                                    richTextBox.SelectionLength += 2;
                                    richTextBox.SelectionColor = Color.Green;
                                }
                            }
                            else
                            {
                                richTextBox.SelectionStart--;
                                richTextBox.SelectionLength += 2;
                                richTextBox.SelectionColor = Color.Red;
                            }
                            richTextBox.SelectionStart += richTextBox.SelectionLength;
                            //richTextBox.SelectionLength = 0;
                            break;
                        }
                    }
                }
            }
            richTextBox.SelectionLength = richTextBox.Text.Length - richTextBox.SelectionStart;
            richTextBox.SelectionColor = Color.Black;
            for (int i = 0; i < _DaNhan.Count; i++)
            {
                KeyDaNhan _temp = (KeyDaNhan)_DaNhan[i];
                richTextBox.SelectionStart = _temp.Start-1;
                richTextBox.SelectionLength = _temp.Length;
                richTextBox.SelectionColor = Color.Red;
            }

            richTextBox.SelectionStart = _start;
            richTextBox.SelectionLength = _length;
        }
    }
}
